import cors from "cors";
import createError from "http-errors";

class CoreMiddleware {
  constructor(whilelistIps) {
    this.whilelistIps = whilelistIps;
  }

  middleware() {
    return cors({
      optionsSuccessStatus: 200,
      credentials: true,
      origin: (origin, callback) => {
        if (!origin || this.whilelistIps.includes(origin)) {
          callback(null, true);
        } else {
          callback(new createError(404, "Not allowed by CORS"));
        }
      },
    });
  }
}

export default CoreMiddleware;
